package com.zoroki.student_score_system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zoroki.student_score_system.domain.User;
import com.zoroki.student_score_system.service.UserService;
import com.zoroki.student_score_system.utils.Msg;
import com.zoroki.student_score_system.vo.Result;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Autowired
    UserService userService;

//    @GetMapping("/detail")
//    public @ResponseBody String detail() {
//        return null;
//    }

    @GetMapping("/getUserInfo")
    @ResponseBody
    public Result<User> getUserInfo(String username) {
        User userInfo = userService.getUserInfo(username);
        if(userInfo != null) {
            userInfo.setPassword(null);
            return new Result<User>(0,userInfo, Msg.SUCCESS);
        }
        return new Result<User>(1,userInfo, Msg.ERROR);
    }

    @GetMapping("/findAllUser")
    public @ResponseBody Result<List<User>> findAllUser() {
        return new Result<>(0,userService.findAll(),Msg.SUCCESS);
    }

    @GetMapping("/getAllStudent")
    public @ResponseBody Result<Page<User>> getAllStudent(int pageNo, int pageSize, String username) {
        return new Result<>(0,userService.getStudentByPage(pageNo,pageSize,username),Msg.SUCCESS);
    }
    @GetMapping("/getAllTeacher")
    public @ResponseBody Result<Page<User>> getAllTeacher(int pageNo, int pageSize, String username) {
        return new Result<>(0,userService.getTeacherByPage(pageNo,pageSize,username),Msg.SUCCESS);
    }
    @GetMapping("/getAllAdmin")
    public @ResponseBody Result<Page<User>> getAllAdmin(int pageNo, int pageSize, String username) {
        return new Result<>(0,userService.getAdminByPage(pageNo,pageSize,username),Msg.SUCCESS);
    }


    @PostMapping("/addUser")
    public @ResponseBody Result<Boolean> addUser(@RequestBody User user) {
        return new Result<Boolean>(0,userService.addUser(user), Msg.SUCCESS);
    }

    @PostMapping("/updateUser")
    public @ResponseBody Result<Boolean> updateUser(@RequestBody User user) {
        System.out.println(user);
        return new Result<Boolean>(0,userService.updateUser(user), Msg.SUCCESS);
    }

    @PostMapping("/deleteUserByUsername")
    public @ResponseBody Result<Boolean> deleteUserByUsername(String username) {
        return new Result<Boolean>(0,userService.deleteUserByUsername(username), Msg.SUCCESS);
    }
}
